package me.zhengjie.ship.shipbase.rest;

import me.zhengjie.aop.log.Log;
import me.zhengjie.ship.shipbase.domain.ShipBase;
import me.zhengjie.ship.shipbase.service.ShipBaseService;
import me.zhengjie.ship.shipbase.service.dto.ShipBaseQueryCriteria;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
* @author liwh
* @date 2020-04-22
*/
@Api(tags = "舰艇基本信息管理")
@RestController
@RequestMapping("/api/shipBase")
public class ShipBaseController {

    private final ShipBaseService shipBaseService;

    public ShipBaseController(ShipBaseService shipBaseService) {
        this.shipBaseService = shipBaseService;
    }

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('shipBase:list')")
    public void download(HttpServletResponse response, ShipBaseQueryCriteria criteria) throws IOException {
        shipBaseService.download(shipBaseService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询舰艇基本信息")
    @ApiOperation("查询舰艇基本信息")
    @PreAuthorize("@el.check('shipBase:list')")
    public ResponseEntity<Object> getShipBases(ShipBaseQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(shipBaseService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增舰艇基本信息")
    @ApiOperation("新增舰艇基本信息")
    @PreAuthorize("@el.check('shipBase:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody ShipBase resources){
        return new ResponseEntity<>(shipBaseService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改舰艇基本信息")
    @ApiOperation("修改舰艇基本信息")
    @PreAuthorize("@el.check('shipBase:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody ShipBase resources){
        shipBaseService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除舰艇基本信息")
    @ApiOperation("删除舰艇基本信息")
    @PreAuthorize("@el.check('shipBase:del')")
    @DeleteMapping
    public ResponseEntity<Object> deleteAll(@RequestBody Integer[] ids) {
        shipBaseService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}